#!/bin/bash
function clearIptables(){
    echo "iptables -F" > /ipf.sh
    if [ $? -ne 0 ]; then
        echo "Failed to generate file."
        return 1
    fi
    echo "Generate file successfully"
    sudo echo "/ipf.sh" >> /etc/rc.d/rc.local
    if [ $? -ne 0 ]; then
        echo "Failed to add scheduled task."
        return 1
    fi
    echo "Successfully added scheduled task"
    sudo chmod +x /etc/rc.d/rc.local
    if [ $? -ne 0 ]; then
        echo "Permission modification failed."
        return 1
    fi
    echo "Successfully started the cleanup rule."
}

function tasks(){
    # Define the command to run
    command="sync; echo 3 > /proc/sys/vm/drop_caches"

    # Define the schedule for the task
    schedule="0 0 * * *"

    # Create the entry in the crontab file
    echo "$schedule $command" | crontab 
    systemctl enable crond 
    systemctl restart crond
}

function optimizeKernel(){
    ulimit -HSn 50000
    echo "" > /etc/sysctl.conf
    echo "net.core.wmem_default =10486760" >> /etc/sysctl.conf
    echo "net.core.wmem_max=26214400" >> /etc/sysctl.conf
    echo "net.core.rmem_default=26214400" >> /etc/sysctl.conf
    echo "net.core.rmem_max=56214400" >> /etc/sysctl.conf
    echo "net.core.netdev_max_backlog = 10000" >> /etc/sysctl.conf
    echo "fs.file-max=500000" >> /etc/sysctl.conf
    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
    #echo "net.ipv4.tcp_congestion_control=bbrplus=1" >> /etc/sysctl.conf
    sysctl -p
    if [ $? -ne 0 ]; then
        echo "Kernel optimization failed."
        return 1
    fi
    echo "Kernel optimization succeeded."
}


function installTools(){
    echo "Installing tools..."
    sudo yum install git wget mtr iftop iperf3 bind-utils tcptraceroute bc -y
    if [ $? -ne 0 ]; then
        echo "Error installing tools."
        return 1
    fi
    echo "Downloading tcping..."
    sudo wget -O //usr/bin/tcping https://soft.mengclaw.com/Bash/TCP-PING --no-check-certificate
    if [ $? -ne 0 ]; then
        echo "Error downloading tcping."
        return 1
    fi
    echo "Setting executable permissions for tcping..."
    sudo chmod a+x /usr/bin/tcping
    if [ $? -ne 0 ]; then
        echo "Error setting permissions for tcping."
        return 1
    fi
    echo "Tools installation complete."
    return 0
}

function setDns(){
    echo "Setting DNS..."
    ip=$(curl http://ip.sb)
    if [ $? -ne 0 ]; then
        echo "Error retrieving IP address."
        return 1
    fi
    dns=$(curl https://www.yifei.cool:8080/jeecg-boot/servic/getDns?ip=$ip)
    if [ $? -ne 0 ]; then
        echo "Error retrieving DNS servers."
        return 1
    fi
    dnss=($(echo $dns | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'))
    echo -e "nameserver ${dnss[0]}
nameserver ${dnss[1]}" >/etc/resolv.conf
    if [ $? -ne 0 ]; then
        echo "Error writing to resolv.conf."
        return 1
    fi
    echo "DNS set."
    return 0
}

function setMtu(){
    echo "Setting MTU..."
    file=/etc/sysconfig/network-scripts/ifcfg-eth0
    mtu=$(grep MTU $file)
    if [ -z $mtu ]; then
        echo "MTU=1500" >> $file
    else
        sed -i "s/$mtu/MTU=1500/" $file
    fi
    if [ $? -ne 0 ]; then
        echo "Error setting MTU."
        return 1
    fi
    echo "Restarting network..."
    systemctl  restart network
    if [ $? -ne 0 ]; then
        echo "Error restarting network."
        return 1
    fi
    echo "MTU set."
    return 0
}

    setMtu
    setDns

function installWgrest(){
    echo "Installing wgrest..."
    curl -L https://github.com/suquant/wgrest/releases/latest/download/wgrest-linux-amd64 -o /usr/bin/wgrest
    if [ $? -ne 0 ]; then
        echo "Error downloading wgrest."
        return 1
    fi
    chmod +x /usr/bin/wgrest

    curl -L https://github.com/suquant/wgrest-webapp/releases/latest/download/webapp.tar.gz -o webapp.tar.gz
    sudo mkdir -p /var/lib/wgrest/
    echo "Setting executable permissions for wgrest..."
    sudo chown `whoami` /var/lib/wgrest/
    tar -xzvf webapp.tar.gz -C /var/lib/wgrest/
    sudo mkdir -p /var/lib/wgrest/
    rm -rf webapp.tar.gz
    echo "[Unit]
Description=wgrest
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
Type=simple
User=root
DynamicUser=true
ExecStart=/usr/bin/wgrest --static-auth-token "secret" --listen "0.0.0.0:8000"

[Install]
WantedBy=multi-user.target">/usr/lib/systemd/system/wgrest.service
systemctl enable wgrest
    if [ $? -ne 0 ]; then
        echo "Network restart failed."
        return 1
    fi
}

function installWg(){  
    setMtu
    setDns
    yum install yum-utils epel-release -y
    if [ $? -ne 0 ]; then
        echo "Error installing yum-utils and epel-release."
        return 1
    fi
    yum-config-manager --setopt=centosplus.includepkgs=kernel-plus --enablerepo=centosplus --save
    if [ $? -ne 0 ]; then
        echo "Error setting yum-config-manager options."
        return 1
    fi
    sed -e 's/^DEFAULTKERNEL=kernel$/DEFAULTKERNEL=kernel-plus/' -i /etc/sysconfig/kernel
    if [ $? -ne 0 ]; then
        echo "Error updating default kernel."
        return 1
    fi
    yum install kernel-plus wireguard-tools -y
    if [ $? -ne 0 ]; then
        echo "Error installing kernel-plus and wireguard-tools."
        return 1
    fi

    echo "[Interface]
Address = 172.30.30.1/32
SaveConfig = true
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51468
PrivateKey = sCK+ns1GH0CxBnKgO5v14o+u51xfIrkiCTeT828dJVI=">/etc/wireguard/wg0.conf
    systemctl enable wg-quick@wg0 --now
    if [ $? -ne 0 ]; then
        echo "Error enabling and starting wg-quick@wg0."
        return 1
    fi
    echo "WireGuard has been successfully installed and started."
}

function replaceSource(){
    rm -rf /etc/yum.repos.d/*
    rpm -Uvh --force http://mirror.centos.org/centos-7/7/os/x86_64/Packages/centos-release-7-9.2009.0.el7.centos.x86_64.rpm
    if [ $? -ne 0 ]; then
        echo "Error updating yum source."
        return 1
    fi
    yum clean all
    if [ $? -ne 0 ]; then
        echo "Error cleaning yum cache."
        return 1
    fi
    yum makecache
    if [ $? -ne 0 ]; then
        echo "Error making yum cache."
        return 1
    fi
    echo "Yum source successfully replaced."
}
replaceSource
installTools
installWg
optimizeKernel
clearIptables
tasks
installWgrest
echo "success"
reboot
